static void
free_texture(void * tex,void * desc)
{
	if (!hge) return;
	if (tex)
	{
		if (desc != (void*)DESC_HTEXTURE)
		{
			hge->System_Log("Error free texture.Desc error.");
			return;
		}
		hge->Texture_Free((HTEXTURE)tex);
	}
}
static PyObject *
pyTexture_Create(PyObject *self,PyObject *args)
{
	CHECK_HGE;
	int w,h;
	if (!PyArg_ParseTuple(args,"ii:Texture_Create",&w,&h))
		return 0;
	HTEXTURE tex = hge->Texture_Create(w,h);
	if (!tex) Py_RETURN_NONE;
	return PyCObject_FromVoidPtrAndDesc((void*)tex,(void*)DESC_HTEXTURE,free_texture);
}
static PyObject *
pyTexture_Load(PyObject *self,PyObject *args)
{
	CHECK_HGE;
	const char * filename;
	DWORD size=0;
	PyObject * bMipmap = Py_False;
	if (!PyArg_ParseTuple(args,"s|IO:Texture_Load",&filename,&size,&bMipmap))
		return 0;
	int bval = PyObject_IsTrue(bMipmap);
	if (bval == -1)  return 0;
	bool mipmap = bval>0;
	HTEXTURE tex = hge->Texture_Load(filename,size,mipmap);
	if (!tex)
	{
		PyErr_SetString(pyHGEError,hge->System_GetErrorMessage());
		return 0;
	}
	return PyCObject_FromVoidPtrAndDesc((void*)tex,(void*)DESC_HTEXTURE,free_texture);
}
static PyObject *
pyTexture_Free(PyObject *self,PyObject *args)
{
	CHECK_HGE;
	PyObject *o;
	if (!PyArg_ParseTuple(args,"O:Texture_Free",&o))
		return 0;
	if (!PyCObject_Check(o) || PyCObject_GetDesc(o)!=(void*)DESC_HTEXTURE)
	{
		PyErr_SetString(PyExc_TypeError,"Texture_Free(): Texture object is required.");
		return 0;
	}
	HTEXTURE tex = (HTEXTURE)PyCObject_AsVoidPtr(o);
	if (!tex)
	{
		//PyErr_SetString(PyExc_ValueError,"Texture object is NULL.");
		//return 0;
		Py_RETURN_NONE;
	}
	hge->Texture_Free(tex);
	PyCObject_SetVoidPtr(o,NULL);
	Py_RETURN_NONE;
}

static PyObject *
pyTexture_GetWidth(PyObject *self,PyObject *args)
{
	CHECK_HGE;
	PyObject *o1,*o2 = Py_False;
	if (!PyArg_ParseTuple(args,"O|O:Texture_GetWidth",&o1,&o2))
		return 0;
	if (!PyCObject_Check(o1) || PyCObject_GetDesc(o1)!=(void*)DESC_HTEXTURE)
	{
		PyErr_SetString(PyExc_TypeError,"Texture object is required.");
		return 0;
	}
	HTEXTURE tex = (HTEXTURE)PyCObject_AsVoidPtr(o1);
	if (!tex)
	{
		//PyErr_SetString(PyExc_ValueError,"Texture object is NULL.");
		//return 0;
		return PyInt_FromLong(0);
	}
	int bval = PyObject_IsTrue(o2);
	if (bval == -1) return 0;
	bool bOrignal = bval>0;
	return PyInt_FromLong(hge->Texture_GetWidth(tex,bOrignal));
}
static PyObject *
pyTexture_GetHeight(PyObject *self,PyObject *args)
{
	CHECK_HGE;
	PyObject *o1,*o2 = Py_False;
	if (!PyArg_ParseTuple(args,"O|O:Texture_GetHeight",&o1,&o2))
		return 0;
	if (!PyCObject_Check(o1) || PyCObject_GetDesc(o1)!=(void*)DESC_HTEXTURE)
	{
		PyErr_SetString(PyExc_TypeError,"Texture object is required.");
		return 0;
	}
	HTEXTURE tex = (HTEXTURE)PyCObject_AsVoidPtr(o1);
	if (!tex)
	{
		//PyErr_SetString(PyExc_ValueError,"Texture object is NULL.");
		//return 0;
		return PyInt_FromLong(0);
	}
	int bval = PyObject_IsTrue(o2);
	if (bval == -1) return 0;
	bool bOrignal = bval>0;
	return PyInt_FromLong(hge->Texture_GetHeight(tex,bOrignal));
}